package com.wu.cloudribbon.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.wu.common.entity.ResultData;
import com.wu.common.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author whx
 * @date 2021/7/10
 */

@RestController
public class TemplateController {

    @Autowired
    RestTemplate restTemplate;
    @Autowired
    LoadBalancerClient loadBalancerClient;
    @Autowired
    DiscoveryClient discoveryClient;

    @GetMapping("/user/list")
//    @HystrixCommand(defaultFallback="back")
    @HystrixCommand(fallbackMethod = "back", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000")
    })
    public ResultData userList(){
        List<String> list = discoveryClient.getServices();
        System.out.println(list.size());
        loadBalancerClient.choose("user-server");
        return restTemplate.getForObject("http://user-server/user/list",ResultData.class);
    }

    public ResultData back(){
        return new ResultData().FAIL("服务熔断");
    }

    @GetMapping("/user/page")
    public ResultData userPageMap(@RequestParam("id") int id,@RequestParam("name") String name){
        loadBalancerClient.choose("user-server");
        Map<String,Object> params = new HashMap<>(2);
        params.put("id",id);
        params.put("name",name);
        return restTemplate.getForObject("http://user-server/user/page?id={id}&name={name}",ResultData.class,params);
    }

    @GetMapping("/user/page2")
    public ResultData userPage(@RequestParam("id") int id,@RequestParam("name") String name){
        loadBalancerClient.choose("user-server");
        return restTemplate.getForObject("http://user-server/user/page?id={1}&name={2}",ResultData.class,id,name);
    }

    @PostMapping("/user/getById")
    public ResultData userGetById(Integer id){
        loadBalancerClient.choose("user-server");
        MultiValueMap<String,Integer> param = new LinkedMultiValueMap<>(1);
        param.add("id",id);
        return restTemplate.postForObject("http://user-server/user/getById",param,ResultData.class);
    }

    @PostMapping("/user/getOne")
    public ResultData userGetOne(){
        loadBalancerClient.choose("user-server");
        return restTemplate.postForObject("http://user-server/user/getOne",null,ResultData.class);
    }

    @PostMapping("/user/getUser")
    public ResultData userGetUser(@RequestBody User user){
        loadBalancerClient.choose("user-server");
        return restTemplate.postForObject("http://user-server/user/getUser",user,ResultData.class);
    }


}
